<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>
description('Tests for .valueAsNumber with &lt;input type=number>.');

var input = document.createElement('input');
input.type = 'number';

function valueAsNumberFor(stringValue) {
    input.value = stringValue;
    return input.valueAsNumber;
}

function setValueAsNumberAndGetValue(num) {
    input.valueAsNumber = num;
    return input.value;
}

shouldBe('valueAsNumberFor("0")', '0');
shouldBe('valueAsNumberFor("0.2")', '0.2');
shouldBe('valueAsNumberFor(".2")', '0.2');
shouldBe('valueAsNumberFor("10")', '10');
shouldBe('valueAsNumberFor("01")', '1');
shouldBe('valueAsNumberFor("-0")', '0'); // "-0" is 0 in HTML5.
shouldBe('valueAsNumberFor("-1.2")', '-1.2');
shouldBe('valueAsNumberFor("1.2E10")', '1.2E10');
shouldBe('valueAsNumberFor("1.2E-10")', '1.2E-10');
shouldBe('valueAsNumberFor("1.2E+10")', '1.2E10');
shouldBe('valueAsNumberFor("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789")', '1.2345678901234568E+308');
shouldBe('valueAsNumberFor("0.12345678901234567890123456789012345678901234567890")', '0.123456789012345678');

debug('valueAsNumber for invalid string values:');
shouldBeTrue('isNaN(valueAsNumberFor(""))');
shouldBeTrue('isNaN(valueAsNumberFor("abc"))');
shouldBeTrue('isNaN(valueAsNumberFor("0xff"))');
shouldBeTrue('isNaN(valueAsNumberFor("+1"))');
shouldBeTrue('isNaN(valueAsNumberFor(" 10"))');
shouldBeTrue('isNaN(valueAsNumberFor("10 "))');
shouldBeTrue('isNaN(valueAsNumberFor("."))');
shouldBeTrue('isNaN(valueAsNumberFor("1E"))');
shouldBeTrue('isNaN(valueAsNumberFor("NaN"))');
shouldBeTrue('isNaN(valueAsNumberFor("nan"))');
shouldBeTrue('isNaN(valueAsNumberFor("Inf"))');
shouldBeTrue('isNaN(valueAsNumberFor("inf"))');
shouldBeTrue('isNaN(valueAsNumberFor("Infinity"))');
shouldBeTrue('isNaN(valueAsNumberFor("infinity"))');

debug('Too huge exponent to support');
shouldBeTrue('isNaN(valueAsNumberFor("1.2E65535"))');

debug('Tests for the valueAsNumber setter:');
shouldBeEqualToString('setValueAsNumberAndGetValue(0)', '0');
shouldBeEqualToString('setValueAsNumberAndGetValue(10)', '10');
shouldBeEqualToString('setValueAsNumberAndGetValue(01)', '1');
shouldBeEqualToString('setValueAsNumberAndGetValue(-0)', '0');
shouldBeEqualToString('setValueAsNumberAndGetValue(-1.2)', '-1.2');
shouldBeEqualToString('setValueAsNumberAndGetValue(1.2e10)', '12000000000');
shouldBeEqualToString('setValueAsNumberAndGetValue(1.2e-10)', '1.2e-10');
shouldBeEqualToString('setValueAsNumberAndGetValue(1.2345678901234567e+308)', '1.2345678901234567e+308');
shouldBeEqualToString('setValueAsNumberAndGetValue("-1.797693134862315e+308")', '-1.797693134862315e+308');
shouldThrow('setValueAsNumberAndGetValue("-1.797693134862316e+308")', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
shouldBe('setValueAsNumberAndGetValue("1.797693134862315e+308")', '"1.797693134862315e+308"');
shouldThrow('setValueAsNumberAndGetValue("1.797693134862316e+308")', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');

debug('Tests to set invalid values to valueAsNumber:');
shouldBeEqualToString('setValueAsNumberAndGetValue(null)', '0');
shouldBeEqualToString('setValueAsNumberAndGetValue("foo")', '');
shouldBeEqualToString('setValueAsNumberAndGetValue(NaN)', '');
shouldBeEqualToString('setValueAsNumberAndGetValue(Number.NaN)', '');
shouldThrow('setValueAsNumberAndGetValue(Infinity)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
shouldThrow('setValueAsNumberAndGetValue(Number.POSITIVE_INFINITY)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
shouldThrow('setValueAsNumberAndGetValue(Number.NEGATIVE_INFINITY)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
</script>
</body>
</html>
